﻿#region Libraries

using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace XYZ_Inventory
{
    public partial class RawMaterialOrdersHistory : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGridView();
        }

        private void LoadGridView()
        {
            XYZDBContext dbContext = new XYZDBContext();
            if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
            {
                int orderId = Convert.ToInt32(txtSearch.Text);
                GridView1.DataSource =
                    dbContext.RawMaterialOrders.Where(l => l.ID == orderId)
                        .Join(dbContext.RawMaterialDeliveries, x => x.ID, y => y.ID, (a, b) => new
                        {
                            a.ID,
                            a.OrderDate,
                            a.DeliveryDate,
                            a.Quantity,
                            a.RawMaterialID,
                            a.SupplierID,
                            b.UnorderedItemQty,
                            b.OverOrderedItemQty,
                            b.UnderOrderedItemQty,
                            b.DamagedItemQty
                        }).OrderByDescending(c => c.ID).ToList();
            }
            else
            {
                GridView1.DataSource =
                    dbContext.RawMaterialOrders.Join(dbContext.RawMaterialDeliveries, x => x.ID, y => y.ID,
                        (a, b) => new
                        {
                            a.ID,
                            a.OrderDate,
                            a.DeliveryDate,
                            a.Quantity,
                            a.RawMaterialID,
                            a.SupplierID,
                            b.UnorderedItemQty,
                            b.OverOrderedItemQty,
                            b.UnderOrderedItemQty,
                            b.DamagedItemQty
                        }).OrderByDescending(c => c.ID).ToList();
            }
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadGridView();
        }
    }
}